<?php
/**
 * Created by PhpStorm.
 * User: yaoihu
 * Date: 2019/6/13
 * Time: 10:18
 */

namespace App\Models\Shop;


use App\Helpers\SelfEncrypt;
use App\Models\BaseModel;
use Carbon\Carbon;
use DC\UserCenter\Models\UserWalletModel;

class AgentSalesModel extends BaseModel
{

    protected $table = 'shop_goods_agent';

    protected $primaryKey = 'id';

    public $dateFormat = 'U';

    public $dates = [
        'created_at',
        'updated_at',
        'started_at',
        'end_at',
        'cycle_at',
    ];

    public function user()
    {
        return $this->belongsTo('DC\UserCenter\Models\UserBaseModel','user_id','member_id');
    }



    public function isOpen($field = 'gw_agent_is_open')
    {
        $week = Carbon::today()->dayOfWeek;
        $is_open = true;
        if(in_array($week,[0,6])||get_rule($field)!=1){
            $is_open = false;
        }

        return $is_open;
    }

    public function hasSales($user_id,$type = 1)
    {
        $num = self::where('user_id',$user_id)->where('agent_type',$type)
            ->whereBetween('created_at',[Carbon::today()->timestamp,Carbon::now()->timestamp])
            ->count();

        return $num;
    }

    public function createXfAgent($user_id,$number=1)
    {
        $pay_price = abs(get_rule('gw_agent_need_score'));
        $now = Carbon::now()->timestamp;
        $model = new  self();
        $model->agent_sn = SelfEncrypt::createSn('XF',14);
        $model->agent_type = 1;
        $model->user_id = $user_id;
        $model->number = $number;
        $model->price = $pay_price;
        $model->pay_money = $pay_price*$number;
        $model->award_xj = get_rule('gw_award1');
        $model->award_gw = get_rule('gw_award2');
        $model->award_xf = get_rule('gw_award3');
        $model->status = 1;
        $model->started_at = $now;
        $model->end_at = 0;
        $model->cycle_at = Carbon::now()->addDay(get_rule('gw_agent_sales_day'))->timestamp;
        if($model->save()){
            return $model->id;
        }

        return false;
    }

    public function createDhAgent($user_id,$number=1)
    {
        $pay_price = abs(get_rule('dh_agent_need_score'));
        $now = Carbon::now()->timestamp;
        $model = new  self();
        $model->agent_sn = SelfEncrypt::createSn('DH',14);
        $model->agent_type = 2;
        $model->user_id = $user_id;
        $model->number = $number;
        $model->price = $pay_price;
        $model->pay_money = $pay_price*$number;
        $model->pay_cash = $pay_price*$number;
        $model->award_xj = get_rule('dh_award1')*$number;
        $model->award_gw = get_rule('dh_award2')*$number;
        $model->award_xf = get_rule('dh_award3')*$number;
        $model->status = 1;
        $model->started_at = $now;
        $model->end_at = 0;
        $model->cycle_at = Carbon::now()->addDay(get_rule('dh_agent_sales_day'))->timestamp;

        if($model->save()){
            return $model->id;
        }

        return false;
    }
}